#include "syncdelicious.h"

SyncDelicious::SyncDelicious(  ) 
{
	// Relative urls used to work with the del.icio.us API.
	QString str1 = "bundle/all?  bundles/delete?  bundle/set?  inbox/get? \
	  		   	   posts/all?  posts/delete?  posts/get?  posts/recent? \
	  		   	   posts/update? tags/get?  tags/rename?";		
	urlList = str1.split(" ");	  		   
	
	// URL parameters for sending data to del.icio.us 
	QString str2 = "bundle count dt description extended new old replace shared tag tags url no yes";
	paramList = str2.split(" ");

	QString str3 = "Done Ok SomethingWentWrong";
	codeList = str3.split(" "); 
	
	utilities = Utilities( );
	
}



bool SyncDelicious::add( QString url, QString description )
{
	return add( url, description, "", "", "", false, true);
}

bool SyncDelicious::add( QString url, QString description, QString extended, QString tags, QString date)
{
	return add( url, description, extended, tags, date, false, true);
}

bool SyncDelicious::add( QString url, 
						 QString description, 
						 QString extended, 
						 QString tags, 
						 QString date, 
						 bool replace, 
						 bool shared )
{
	if ( url.length() == 0 || description.length() == 0)
	{
		return false;
	}

	QString queryUrl = urlList[ POSTS_ADD ];
	queryUrl = utilities.addParameter( queryUrl, paramList[ URL ], url);
	queryUrl = utilities.addParameter( queryUrl, paramList[ DESCRIPTION ], description);
	
	if( extended.length( ) > 0 )
	{
		queryUrl = utilities.addParameter( queryUrl, paramList[ EXTENDED ], extended);
	}
	if( tags.length( ) > 0 )
	{
		queryUrl = utilities.addParameter( queryUrl, paramList[ TAGS ], tags);
	}
	if( date.length( ) > 0){ // TODO: parse date to make sure it is valid
		queryUrl = utilities.addParameter( queryUrl, paramList[ DATE ], date);
	}
	queryUrl = utilities.addParameter( queryUrl, paramList[ REPLACE ],
										 ((replace) ? paramList[ YES ]: paramList[ NO ]));
	if (!shared){
		queryUrl = utilities.addParameter( queryUrl, paramList[ SHARED ], paramList[ NO ]);
	}
	
	return true;
}
